package nl.hanze.web.t41.http;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

public class HTTPResponse {
	
//	private HTTPHeader header;
		
	public HTTPResponse(OutputStream out, HTTPHeader header, File file){
		
		DataOutputStream output = new DataOutputStream(out);
		FileInputStream requestedfile = null;
		
		try {
			requestedfile = new FileInputStream(file.getAbsolutePath());
			output.writeBytes(header.getHeader());
			
			while (true) {
				int b = requestedfile.read();
		        if (b == -1) {
		            break; //end of file
		        }
		        output.write(b);
			}
			

			output.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		// try to send an error page including the header
		//PrintWriter pw = new PrintWriter(out);
		//pw.println(header.getHeader()); // the http header
		//pw.println(body.getBody()); // the body
		//pw.flush();
		
		// check header in terminal
		System.out.println("HEADER:\r\n\r\n" + header.getHeader() + "\r\n");
	}
	
	
}
